<!-- 入库商品 -->
<template>
  <div class="goods_con">
    <el-dialog title="选择商品" width="700px" :visible.sync="innerVisible" :close-on-click-modal="false" :show-close='false' append-to-body>
      <el-form class="search" label-position="right">
        <el-row :gutter="40">
          <el-col :span="8">
            <el-form-item label="商品类目:">
              <!-- <el-cascader v-model="fansParams.goodsCategoryId" :options="treeList" :props="{ value: 'id', label: 'name' }" @change="changeGoodsCategoryId" /> -->
              <el-cascader v-model="categoryIdArr" :options="treeList" :props="{ value: 'id', label: 'name' }" @change="changeGoodsCategoryId" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品名称:">
              <el-input v-model="fansParams.name" />
            </el-form-item>
          </el-col>

          <el-col :span="8" class="mb15">
            <SearchButton :default-params="fansParams" @refreshParams="refreshFansList" @getList="getFansList" />
          </el-col>
        </el-row>
        <vxe-table
          ref="listTree"
          border
          resizable
          highlight-hover-row
          :auto-resize="true"
          :data="fansList"
          width
          max-height="300"
          :print-config="{}"
          row-id="id"
          highlight-current-row
          text-algin="center"
          :radio-config="{highlight:true}"
          :checkbox-config="{checkRowKeys: defaultCheckIds, checkStrictly: false}"
          @checkbox-change="selectChangeEvent"
          @checkbox-all="selectAll"
          @radio-change="radioChange"
        >
          <vxe-table-column v-if="inputType === 'radio'" type="radio" width="60" />
          <vxe-table-column v-else type="checkbox" align="center" width="60" />
          <vxe-table-column width="150" field="name" title="商品名" />
          <vxe-table-column width="150" field="goodsCategory" title="商品类目" />
          <vxe-table-column  width="100" field="unit" title="单位" />
          <vxe-table-column field="language" title="商品状态">
            <template slot-scope="scope">
              {{ scope.row.status==3?'已下架':'已上架' }}
            </template>
          </vxe-table-column>
        </vxe-table>
        <vxe-pager
          :current-page.sync="fansParams.pageNum"
          :page-size.sync="fansParams.pageSize"
          :total="fansTotal"
          :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'FullJump', 'Total']"
          @page-change="getFansList(fansParams.pageNum)"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeModel">取 消</el-button>
        <el-button type="primary" v-if="inputType == 'radio'" @click="radioClick">确定</el-button>
        <el-button v-else type="primary" @click="chooseFans">
          确定
        </el-button>
      </div>
    </el-dialog>
    <div class="sku_con">
      <el-dialog title="sku" :close-on-click-modal="false" :visible.sync="skuDialog" append-to-body>
        <el-table :data="skuForm" highlight-current-row border style="width: 100%" @selection-change="handleCurrentChange1">
          <!-- <el-table-column
            type="index"
            width="50">
          </el-table-column> -->
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column v-for="(item, index) in listName" :key="index" prop="name" :label="item" min-width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.specParams[index].specValue }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="销售价格" min-width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.retailPrice +"元" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="商品库存" min-width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.stock }}</span>
            </template>
          </el-table-column>

        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="skuDialog = false">关 闭</el-button>
          <el-button @click="choiseSku">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import { goodsLists } from '@/api/shop'
import SearchButton from '@/components/SearchButton'
import { searchGoodsSku } from '@/api/mall/goodsManagement'
import { categoryTree } from '@/api/public'
export default {
  components: {
    SearchButton
  },
  props: {
    checkIds: {
      type: Array,
      default: []
    },
    receiveGoodsList: {
      type: Array,
      default: []
    },
    // 弹窗显示隐藏
    innerVisible: {
      type: Boolean,
      default: false
    },
    // 多个商品模块(类型) 1-实物商品 2-停车券4大码商品
    goodsModuleList: {
      type: Array,
      default: []
    },
    // 多个值 1商品列表，2积分商城商品，3pos商品
    channels: {
      type: Array,
      default: []
    },
    // 单选或者多选
    inputType: {
      type: String,
      default: 'radio'
    },
    // 是否需要选到规格
    isSku: {
      type: String,
      default: '0'
    }
  },
  data() {
    return {
      newGoodsName: '',
      isSkuis:this.isSku,
      // 分页总数
      fansTotal: 1,
      // 搜索条件
      fansParams: {
        // 商品类目
        goodsCategoryId: '',
        // 商品名称
        name: '',
        // 选择商品列表分页
        pageNum: 1,
        pageSize: 10
      },
      skuForm:{},
      skuForm1: {},
      // 商品类目列表
      treeList: [],
      // 选择商品列表
      fansList: [],
      currentRow1: {},
      currentRow: {},
      checkList: [],
      defaultCheckIds: [],
      list1: [],
      listIndex: 0,
      listName1: [],
      listName: [],
      skuDialog1: false,
      skuDialog: false,
      radioObj:{},
      categoryIdArr:[]
    }
  },
  mounted() {
    this.defaultCheckIds =JSON.parse(JSON.stringify(this.checkIds))
    // this.checkList = this.receiveGoodsList
    // const checkList = this.receiveGoodsList.map(item => item.id)
    // this.checkList = JSON.parse(JSON.stringify(checkList))
    // console.log('this.defaultCheckIds', this.defaultCheckIds)
    // this.fansParams.goodsModuleList = this.goodsModuleList
    this.fansParams.channels = this.channels
    this.getFansList()
    this.categoryTree()
  },
  methods: {
    categoryTree() {
      categoryTree({}).then(res => {
        this.treeList = res
      })
    },
    // 全部选中
    selectAll(e) {
      if (e.checked) {
        this.checkList.push(...e.records)
        this.defaultCheckIds.push(...e.records.map((item) => item.id))
        this.checkList = Array.from(new Set(this.checkList))
        this.defaultCheckIds = Array.from(new Set(this.defaultCheckIds))
      } else {
        const arr = this.list.map((item) => item.id)
        const arr1 = []
        this.checkList.forEach((item, index) => {
          arr.forEach((it) => {
            if (item.id === it) {
              arr1.push(index)
            }
          })
        })
        arr1.reverse().forEach((item) => {
          this.checkList.splice(item, 1)
          this.defaultCheckIds.splice(item, 1)
        })
      }
    },
    selectChangeEvent(row) {
      let i = -1
      this.checkList.forEach((item, index) => {
        if (item.id === row.row.id) {
          i = index
        }
      })
      if (i === -1) {
        this.checkList.push(row.row)
        this.defaultCheckIds.push(row.row.id)
      } else {
        this.checkList.splice(i, 1)
        this.defaultCheckIds.splice(i, 1)
      }
    },
    radioChange(row){
      this.radioObj = row.row
    },
    radioClick(){
      this.$emit('getGoodsInfoList',this.radioObj)
    },
    handleCurrentChange1(val) {
      this.currentRow1 = val
    },
    choiseSku() {
      var jsSpecValue = ''
      this.currentRow1.forEach((item,index) => {
        jsSpecValue = ''
        item.specParams.forEach((items,i) => {
         jsSpecValue += items.specValue + '-'
        item.jsSpecValue = jsSpecValue.slice(0,-1)
        item.name = this.newGoodsName
        })
      })
      const goodsData = {
        tableData: this.currentRow1,
        innerVisible: false
      }
      this.$emit('getGoodsInfoList',goodsData)
      // // this.form.goodDesc = sku_str_id.slice(0, -1)
      // this.$set(this.fansList[this.listIndex], 'goodDesc', tableStr)
      this.skuDialog = false
    },
    closeModel() {
      const goodsData = {
        tableData: [],
        innerVisible: false
      }
      this.$emit('getGoodsInfoList',goodsData)

    },
    // 打开库存弹窗
    openSkuDialog1(id) {
      searchGoodsSku({ id }).then(res => {
        this.skuForm1 = res
        const arr1 = []
        res[0].specParams.forEach((item, index) => {
          arr1.push(item.specName)
        })
        res.forEach(item => {
          item.retailPrice = item.retailPrice ? filterPriceInput(item.retailPrice) : 0
          item.stock = item.stock ? filterInputNumber(item.stock) : 0
          item.warnStock = item.warnStock ? filterInputNumber(item.warnStock) : 0
          item.purchasePrice = item.purchasePrice ? filterPriceInput(item.purchasePrice) : 0
        })
        this.listName1 = arr1
        this.skuDialog1 = true
      })
    },
    // 打开规格弹窗
    openSkuDialog(id, index,name) {
      this.newGoodsName = name
      this.listIndex = index
      searchGoodsSku({ id }).then(res => {
        this.skuForm = res
        const arr1 = []
        res[0].specParams.forEach((item, index) => {
          arr1.push(item.specName)
        })
        this.listName = arr1
        console.log('this.listName',this.listName)
        this.skuDialog = true
      })
    },
    getFansList(num = 1) {
      this.fansParams.pageNum = num
      goodsLists(this.fansParams).then(res => {
        this.innerVisible = true
        // this.$nextTick(() => {
        //   this.checkList.forEach((item, index) => {
        //     res.records.forEach((it, idx) => {
        //       if (item === it.openId) {
        //         this.$refs.listTree.setCheckboxRow(res.records[idx], true)
        //       }
        //     })
        //   })
        this.fansList = res.data.records
        this.fansTotal = res.data.total
        this.$refs.listTree.reloadData(this.fansList)

        // })
      })
    },
    changeGoodsCategoryId() {
      this.fansParams.goodsCategoryId = this.categoryIdArr[this.categoryIdArr.length - 1]
    },
    handleCurrentChange(val) {
      this.currentRow = val.records

    },
    chooseFans() {
      if (this.isSku === '0' && !this.currentRow.goodDesc) {
        return this.$message.warning('请先选择规格!')
      }
      let goodsData = {
        rowIds: this.defaultCheckIds,
        goodsArr: this.checkList,
        innerVisible: false
      }
      this.$emit('getGoodsInfoList',goodsData)
    },
    refreshFansList(params) {
      this.fansParams =  params
      this.categoryIdArr = []
      this.getFansList(1)
    },
  }
}
</script>

<style lang="css" scoped>
</style>
